package cn.acewill.yxd.yunxundian.ui.user;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.componet.NetWorkStateReceiver;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.MainActivity;
import cn.acewill.yxd.yunxundian.ui.sign.LookEvaDetialActivity;
import cn.acewill.yxd.yunxundian.utils.Eyes;

/**
 * Created by xxx on 2018/1/3.
 */

public class EfficManagerH5 extends AppCompatActivity implements View.OnClickListener{
    private ProgressBar myProgressBar;
    String url;
    WebView webView;
    private TextView titleText;
    private ArrayList<String> loadHistoryUrls = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Eyes.setStatusBarLightMode(this,ContextCompat.getColor(this,R.color.white));//android 6.0
        setContentView(R.layout.effic_h5_view);
        initView();
    }

    private void initView(){
//        Toolbar toolbar = (Toolbar) findViewById(R.id.myToolBar);
//        toolbar.setTitle("");
//        toolbar.setNavigationIcon(R.mipmap.back);
//        setSupportActionBar(toolbar);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
        ImageView viewBack = (ImageView) findViewById(R.id.viewBack);
        viewBack.setOnClickListener(this);
        ImageView closebtn = (ImageView) findViewById(R.id.closeBtn);
        closebtn.setOnClickListener(this);
        titleText = (TextView) findViewById(R.id.titleText);
        titleText.setSelected(true);
        webView = (WebView) findViewById(R.id.effView);
        myProgressBar = (ProgressBar) findViewById(R.id.rvPbar);
        url = pubClass.Effic_Server+ MainActivity.id+"&"+"fromApp=1/index.html";
        Log.e("url",url);
        loadHistoryUrls.add(url);
        webView.loadUrl(url);
        webView.getSettings().setJavaScriptEnabled(true);// 支持运行javascript
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setWebChromeClient(new WebChromeClient()
                                   {
                                       @Override
                                       public void onProgressChanged(WebView view, int newProgress) {
                                           if (newProgress == 100) {
                                               myProgressBar.setVisibility(View.GONE);
                                           } else {
                                               if (View.GONE == myProgressBar.getVisibility()) {
                                                   myProgressBar.setVisibility(View.VISIBLE);
                                               }
                                               myProgressBar.setProgress(newProgress);
                                           }
                                           super.onProgressChanged(view, newProgress);
                                       }
                                   }
        );// 支持运行特殊的javascript（例如：alert()）

        webView.setWebViewClient(new ExplanWebViewClinet());// 当点击超链地址后不会新打开浏览器来访问，而是始终在本app中浏览页面
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.viewBack:
                if (loadHistoryUrls.size()>1){
                    loadHistoryUrls.remove(loadHistoryUrls.get(loadHistoryUrls.size()-1));
                    //加载重定向之前的页
                    webView.loadUrl(loadHistoryUrls.get(loadHistoryUrls.size() - 1));
                }else{
                    finish();
                }
                break;
            case R.id.closeBtn:
                finish();
                break;
        }
    }

    private class ExplanWebViewClinet extends WebViewClient{
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            loadHistoryUrls.add(url);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            titleText.setText(webView.getTitle());
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }
    }


    //设置返回键动作（防止按返回键直接退出程序)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if(keyCode== KeyEvent.KEYCODE_BACK) {
//            if(webView.canGoBack()) {//当webview不是处于第一页面时，返回上一个页面
//                webView.goBack();
//                return true;
//            }
//            else {//当webview处于第一页面时,直接退出程序
//                finish();
//            }
//        }
        if (loadHistoryUrls.size()>1&&event.getKeyCode()==KeyEvent.KEYCODE_BACK){
            loadHistoryUrls.remove(loadHistoryUrls.get(loadHistoryUrls.size()-1));
            //加载重定向之前的页
            webView.loadUrl(loadHistoryUrls.get(loadHistoryUrls.size() - 1));
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



}
